<template>
  <div>
    <div>
      <Form
        ref="formValidate"
        :model="formValidate"
        :rules="ruleValidate"
        :label-width="80"
      >
        <Row justify="space-between">
          <Col span="6">
            <FormItem label="入库单号" prop="did">
              <Input
                v-model="formValidate.did"
                placeholder="输入入库单编号"
                disabled
              ></Input>
            </FormItem>
          </Col>
          <!-- <Col span="6">
            <FormItem label="订单号" prop="oid">
              <Input
                v-model="formValidate.oid"
                placeholder="输入订单号"
              ></Input>
            </FormItem>
          </Col> -->
          <Col span="6">
            <FormItem label="入库人" prop="staff">
              <Input v-model="formValidate.staff" disabled />
            </FormItem>
          </Col>
          <Col span="6">
            <FormItem label="入库时间" prop="date">
              <Input v-model="formValidate.date" disabled />
            </FormItem>
          </Col>
        </Row>
        <Row justify="space-between">
          <Col span="6">
            <FormItem label="仓库号" prop="sid">
              <Select
                v-model="formValidate.sid"
                clearable
                filterable
                placeholder="选择仓库号"
              >
                <Option
                  v-for="item in sids"
                  :value="item.value"
                  :key="item.value"
                  >{{ item.value }}</Option
                >
              </Select>
            </FormItem>
          </Col>
          <Col span="6">
            <FormItem label="作物号" prop="cid">
              <Select
                v-model="formValidate.cid"
                clearable
                filterable
                placeholder="选择作物号"
              >
                <Option
                  v-for="item in cids"
                  :value="item.value"
                  :key="item.value"
                  >{{ item.value }}</Option
                >
              </Select>
            </FormItem>
          </Col>
          <Col span="6">
            <FormItem label="批号" prop="batch">
              <Input
                v-model="formValidate.batch"
                placeholder="输入批号"
              ></Input>
            </FormItem>
          </Col>
        </Row>
        <Row justify="space-between">
          <Col span="6">
            <FormItem label="数量" prop="num">
              <InputNumber :min="1" v-model="formValidate.num"></InputNumber>
            </FormItem>
          </Col>
          <COl>
            <FormItem label="单价" prop="price">
              <InputNumber
                :min="0"
                :step="0.1"
                v-model="formValidate.price"
              ></InputNumber>
            </FormItem>
          </COl>
          <Col span="6">
            <FormItem label="备注" prop="desc">
              <Input
                v-model="formValidate.desc"
                type="textarea"
                :autosize="{ minRows: 2, maxRows: 5 }"
                placeholder="Enter something..."
              ></Input>
            </FormItem>
          </Col>
        </Row>
        <FormItem>
          <Button type="primary" @click="handleSubmit('formValidate')"
            >提交</Button
          >
          <Button @click="handleReset('formValidate')" style="margin-left: 8px"
            >重置</Button
          >
        </FormItem>
      </Form>
    </div>
    <div>
      <rk :datas="datas" @removeone="removeone" />
      <Form>
        <FormItem>
          <Button type="primary" @click="confirm">确认</Button>
          <Button @click="clear" style="margin-left: 8px">清空</Button>
        </FormItem>
      </Form>
      <bill :datas="billdata" billname="入库单" :didn="formValidate.did" :shows="sw" @c1="cc" @c2="c2" />
    </div>
  </div>
</template>
<script>
import rk from "../from/incharts.vue";
import bill from './bill.vue'
export default {
  data() {
    return {
      sw:false,
      billdata:[],
      sids: [],
      cids: [],
      datas: [],
      formValidate: {
        did: "0000000001",
        oid: "",
        staff: JSON.parse(sessionStorage.getItem("user")).name,
        sid: "",
        cid: "",
        batch: "",
        num: 0,
        date: "",
        desc: "",
        price:0
      },
      ruleValidate: {
        did: [
          {
            required: true,
            message: "出库单号不能为空",
            trigger: "blur",
          },
        ],
        oid: [
          {
            required: true,
            message: "订单号不能为空",
            trigger: "blur",
          },
        ],
        staff: [
          {
            required: true,
            message: "选择出库员姓名",
            trigger: "change",
          },
        ],

        sid: [
          {
            required: true,
            type: "string",
            message: "请选择仓库",
            trigger: "change",
          },
        ],
        cid: [
          {
            required: true,
            type: "string",
            message: "请选择作物",
            trigger: "change",
          },
        ],
        batch: [
          {
            required: true,
            type: "string",
            message: "请选择批次",
            trigger: "change",
          },
        ],
        num: [
          {
            required: true,
            message: "请输入正确的数量",
            trigger: "blur",
            type: "number",
          },
        ],
      },
    };
  },
  components: {
    rk,
    bill,
  },
  mounted() {
    var today = new Date();
      var y = today.getFullYear();
      var m = today.getMonth() + 1;
      var d = today.getDate();
      m = m < 10 ? "0" + m : m;
      d = d < 10 ? "0" + d : d;
      this.formValidate.date = y + "-" + m + "-" + d;
    this.axios
      .get("/api/sc")
      .then((r) => {
        this.sids = r.data.stores.sids;
        this.cids = r.data.crops.cids;
      })
      .catch((e) => {
        this.$Message.error("E" + e);
      });
      this.axios.post('/api/create',{ctype:"cdid",type:"入库",city:""}).then((r)=>{
        this.formValidate.did=r.data
      }).
      catch((e)=>{
        this.$Message.error("e"+e)
      })
  },
  methods: {
    handleSubmit(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          // this.formValidate.date = Date.now();
          var one = JSON.parse(JSON.stringify(this.formValidate));
          this.datas.push(one);
          this.$Message.success("Success!");
        } else {
          this.$Message.error("Fail!");
        }
      });
    },
    cc(data){
      this.sw=data
    },
    c2(val){
      this.sw=val
    },
    handleReset(name) {
      this.$refs[name].resetFields();
    },

    confirm() {
      var to_tornado = { state: "入库", items: [],infos:[]};
      var to_chiledren=[]
      for (var it in this.datas) {
        var one = {
          sid: this.datas[it].sid,
          cid: this.datas[it].cid,
          batch: this.datas[it].batch,
          instorge: this.datas[it].date,
          num: this.datas[it].num,
          price:this.datas[it].price,
        };
        var onebill=JSON.parse(JSON.stringify(one))
        onebill.name=one.cid
        onebill.total=one.num*one.price
        
        var info={
          did:this.formValidate.did,
          staff:this.formValidate.staff,
          type:"入库",
          remark:this.formValidate.desc
        }
        to_tornado.infos.push(info)
        to_tornado.items.push(one);
        to_chiledren.push(onebill)
      }
      this.axios.put("/api/storge",to_tornado).then(() => {
        this.$Message.info("提交成功");
        this.datas = [];
      }).catch((e)=>{
        this.$Message.error("e"+e)
      });
      this.billdata=to_chiledren
      this.sw=true 
    },

    clear() {
      this.datas = [];
    },

    removeone(index) {
      console.log(index);
      this.datas.splice(index, 1);
    },
  },
};
</script>
